/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import utilitarios.ConnectionHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import pojo.Curso;

/**
 *
 * @author gu
 */
public class DaoCurso implements DAO<Curso> {

    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;
    Curso curso;
    String sql;

    public DaoCurso() {
        conn = ConnectionHelper.getIntance().getConnection();
    }
    
    
    
    @Override
    public void commit(Curso beans) throws SQLException {
        sql = "insert into curso (cu_descricao,cu_carga) values (?,?)";
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, beans.getDescricao());
        pstm.setString(2, beans.getCarga());
        
        pstm.execute();
    }

    @Override
    public Curso findByName(String name) throws SQLException {
        sql = "select cu_id, cu_descricao, cu_carga from curso where cu_descricao LIKE ?"; 
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, name);
        pstm.execute();
        rs = pstm.getGeneratedKeys();
        
        curso.setId(rs.getLong("cu_id"));
        curso.setDescricao(rs.getString("cu_descricao"));
        curso.setCarga(rs.getString("cu_carga"));
        
        return curso;
    }

    @Override
    public void delete(int id) {
        try {
            sql = "delete from curso where al_id = ?";
            pstm = conn.prepareStatement(sql);
            
            pstm.setLong(1, id);
            pstm.execute();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao deletar Curso!");
        }
    }

    @Override
    public void update(Curso entidade) throws SQLException {
        sql = "UPDATE curso SET cu_descricao = ?, cu_carga = ? WHERE cu_id = ?";
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, entidade.getDescricao());
        pstm.setString(3, entidade.getCarga());
        pstm.setLong(4, entidade.getId());
        pstm.execute();
    }
    
    
}
